
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@poppinss/events
Advanced tools
Async event emitter with support for typed events
This module is extracted from AdonisJs. It is an Async event emitter with support for Typed events when using typescript.
Install the package from npm registry as follows:
npm i @poppinss/events
# yarn
yarn add @poppinss/events
And then use it as follows:
import { Emitter } from '@poppinss/events'
const emitter = new Emitter()
emitter.on('signup', async (data) => {
//
})
emitter.emit('signup', { id: 1, email: 'foo@bar.com' })
Node.js inbuilt event emitter is synchronous and blocks the event loop while emitting events. On the other hand, this module relies on emittery to emit events asynchronously.
When using Typescript, you can attach data types to a event name and the Typescript compiler will complain, if types aren't the same.
import { Emitter } from '@poppinss/events'
type EventsMap = {
'new:user': { id: number },
}
const emitter = new Emitter<EventsMap>()
emitter.emit('new:user', { id: 1 }) // works
emitter.emit('new:user', 1) // compiler error
Just like the emit
method, all other methods enforce types on typed events.
const emitter = new Emitter<EventsMap>()
emitter.on('new:user', (user) => {
})
emitter.once('new:user', (user) => {
})
When writing tests, you may want to suppress the events and instead run assertions to ensure that your code is emitting right events with correct data.
You can do this by using the FakeEmitter
instance during tests.
import { EmitterContract } from '@poppinss/events'
export default class UserController {
constructor (protected emitter) {}
create () {
const user = makeSomeDbCall()
this.emitter.emit('new:user', user)
}
}
When writing tests, you can pass the FakeEmitter
, instead of the real emitter instance.
import { FakeEmitter } from '@poppinss/events'
import UserController from './UserController'
const emitter = new FakeEmitter()
new UserController(emitter).create()
assert.deepEqual(emitter.events, [{
event: 'new:user',
data: user,
}])
// Clear stored events inside memory
emitter.clear()
When using with AdonisJs, you can also define listeners as a reference to the IoC container binding. For example:
// Add event listener
Event.on('new:user', 'User.signup')
// Remove event listener
Event.off('new:user', 'User.signup')
AdonisJs will make an instance of app/Listeners/User.ts
file and will call the signup method on it. This keeps your events file clean, since you can abstract the listeners code to dedicated files.
Also, you can define a custom namespace to lookup listeners from.
Event.namespace('App/MyListeners')
Now AdonisJs will look inside the MyListeners
directory vs Listeners
.
Following are the autogenerated files via Typedoc
FAQs
Event emitter with asynchronous events
We found that @poppinss/events demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.